/*
 * File Name：InstallActivity.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： InstallActivity.java
 * Modify By：bin
 * Modify Date：2015年4月23日
 * Modify Type：Add
 */
package com.ciwong.mobilelib.ui;

import android.Manifest;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;

import com.ciwong.libs.utils.CWLog;
import com.ciwong.mobilelib.R;
import com.ciwong.mobilelib.utils.CWUpdate;
import com.ciwong.mobilelib.utils.XXPermissionTool;
import com.ciwong.mobilelib.widget.CWDialog;
import com.ciwong.mobilelib.utils.fileprovider.FileProvider7;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 安装APK
 *
 * @author bin
 * @version ciwong v.1.0 2015年4月23日
 * @since ciwong v.1.0
 */
public class InstallActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        showGetInstallPermissDialog();
    }

    private void initCreate() {
        isNeedCheck = false;
        NotificationManager mNotificationManager = CWUpdate.UpdateVersion
                .getNotificationManager();
        if (mNotificationManager != null) {
            mNotificationManager.cancel(CWUpdate.UpdateVersion.ID);
        }
        String apkFilePath = getIntent().getStringExtra(
                CWUpdate.UpdateVersion.APK_FILE_PATH);
        if (apkFilePath != null && !"".equals(apkFilePath)) {
            File file = new File(apkFilePath);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            FileProvider7.setIntentDataAndType(this,
                    intent, "application/vnd.android.package-archive", file, true);
            startActivityForResult(intent, 0);
        }
        finish();
    }

    /**
     * 判断是否需要检测，防止不停的弹框
     */
    protected boolean isNeedCheck = true;
    private final int CODE_REQUEST_PERMISSION = 9900;
    private static final String TAG = "InstallActivity";

    private CWDialog getSDcardPermissDialog;

    /**
     * 获取SDcard权限提示框
     */
    private boolean showGetInstallPermissDialog() {
        boolean isHavePermiss = XXPermissionTool.isHasPermission(this, Manifest.permission.REQUEST_INSTALL_PACKAGES);
        if (isHavePermiss) {
            checkInstallPermissions();
        } else {
            if (getSDcardPermissDialog == null) {
                getSDcardPermissDialog = new CWDialog(this);
                getSDcardPermissDialog.setTitle(R.string.get_permiss_title);
                getSDcardPermissDialog.setTitleTextColor(Color.BLACK);
                getSDcardPermissDialog.setMessage(getString(R.string.get_install_permiss_content), 16, Color.BLACK, Gravity.LEFT);
                getSDcardPermissDialog.setPositiveButton(R.string.get_permiss_ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        getSDcardPermissDialog.dismiss();
                        checkInstallPermissions();
                    }
                });
                getSDcardPermissDialog.setNegativeButton(R.string.get_permiss_no, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        getSDcardPermissDialog.dismiss();
                    }
                });
            }
            getSDcardPermissDialog.show();
        }
        return true;
    }

    /**
     * XXPermissions方法是请求某一权限，
     */
    private void checkInstallPermissions() {
        List<String> permissionList = new ArrayList<String>();
        Map<String, String> noteMap = new HashMap<>();
        permissionList.add(Manifest.permission.REQUEST_INSTALL_PACKAGES);
        noteMap.put(Manifest.permission.REQUEST_INSTALL_PACKAGES, "开启安装应用权限，可以升级安装到最新版本。");
        XXPermissionTool.checkPermissions(this, //
                permissionList, //
                noteMap, //
                CODE_REQUEST_PERMISSION, //
                new XXPermissionTool.OnCheckPermissionsFinishedListener() {
                    @Override
                    public void onCheckPermissionsFinished(int flag) {
                        if (0 == flag) {
                            initCreate();
                        } else {
                            finish();
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        CWLog.e("InstallActivity", "requestCode=" + requestCode + "&resultCode=" + resultCode);
        if (requestCode == CODE_REQUEST_PERMISSION) {
            Log.e("SplashActivity", "再次检查权限");
            checkInstallPermissions();
        }
    }
}
